import requests
from bs4 import BeautifulSoup
import time

url = "https://www.umeituku.com/bizhitupian/weimeibizhi/"

resp = requests.get(url)

resp.encoding = "utf-8"

# print(resp.text)

main_page = BeautifulSoup(resp.text,"html.parser")
alist = main_page.find("div",class_="TypeList").find_all("a")
# print(alist)

for a in alist:
    href = a.get("href")
    #拿到子页面的源代码
    child_href_resp = requests.get(href)
    child_href_resp.encoding = "utf-8"
    child_href_text = child_href_resp.text
    #从子页面拿到图片下载路径
    child_page = BeautifulSoup(child_href_text,"html.parser")
    p = child_page.find("div" ,class_= "ImageBody")
    img = p.find("img")
    src = img.get("src")
    #下载图片
    img_resp = requests.get(src)
    img_name = src.split("/")[-1]

    with open("img/"+img_name,mode="wb") as  f:
        f.write(img_resp.content)#图片写入文件

    print("over!!",img_name)
    time.sleep(1)

print("all over")



